import { reqgoodscount, reqgoodslist } from "../../http/api"
const state = {
    list: [],
    total: 0,
    size: 2,
    page: 1
}
const getters = {
    list(state) {
        return state.list
    },
    total(state) {
        return state.total
    },
    size(state) {
        return state.size
    },
}
const mutations = {
    changeList(state, list) {
        state.list = list
    },
    changeTotal(state, total) {
        state.total = total;
    },
    changePage(state, page) {
        state.page = page;
    }

}
const actions = {
    reqList({state:{size,page},commit,dispatch}){
        reqgoodslist({size,page}).then((res)=>{
            console.log(res);
            if(res.data.code == 200){
                var arr = res.data.list ? res.data.list:[];
                if(arr.length == 0 && page != 1){
                    commit("changePage",page - 1)
                    dispatch("reqList")
                    return;
                }
                commit("changeList", arr)
            }
        })
    },
    
    //请求总和
    reqTotal({ commit }) {
        reqgoodscount().then((res) => {
            if (res.data.code == 200) {
                commit("changeTotal", res.data.list[0].total)
            }
        })
    },
    /* 修改页码 */
    changePage({ commit, dispatch }, page) {
        commit("changePage", page);
        dispatch("reqList")
    }

}
export default {
    state,
    getters,
    mutations,
    actions,
    namespaced: true
}